package com.chuzhong.sf.demo;

import sun.misc.IOUtils;
import sun.nio.ch.IOUtil;

import java.io.*;
import java.net.*;
import java.util.Date;

/**
 * @Author 初衷.
 * @Date 2020/10/15
 **/
public class demo {

    /*http://114.215.198.145/city
    * 接口
    * */
    public static void main(String[] args) throws IOException {
        String username = "";
        boolean equals = username.equals(null);
        System.out.println(equals);

//        a("http://114.215.198.145/city");
}
public static  void b(String pathUrl, String data){
    OutputStreamWriter out = null;
    BufferedReader br = null;
    String result = "";
    try {
        URL url = new URL(pathUrl);
        //打开和url之间的连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //请求方式
//        conn.setRequestMethod("POST");
        conn.setRequestMethod("GET");

        //设置通用的请求属性
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");

        //DoOutput设置是否向httpUrlConnection输出，DoInput设置是否从httpUrlConnection读入，此外发送post请求必须设置这两个
        conn.setDoOutput(true);
        conn.setDoInput(true);

        /**
         * 下面的三句代码，就是调用第三方http接口
         */
        //获取URLConnection对象对应的输出流
        out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        //发送请求参数即数据
        out.write(data);
        //flush输出流的缓冲
        out.flush();

        /**
         * 下面的代码相当于，获取调用第三方http接口后返回的结果
         */
        //获取URLConnection对象对应的输入流
        InputStream is = conn.getInputStream();
        //构造一个字符流缓存
        br = new BufferedReader(new InputStreamReader(is));
        String str = "";
        while ((str = br.readLine()) != null){
            result += str;
        }
        System.out.println(result);
        //关闭流
        is.close();
        //断开连接，disconnect是在底层tcp socket链接空闲时才切断，如果正在被其他线程使用就不切断。
        conn.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            if (out != null){
                out.close();
            }
            if (br != null){
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static String a(String Url){

    try {
        // 传入参数
        String realUrl = Url;
        URL url = new URL(realUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 在连接之前设置属性

        // Content-Type实体头用于向接收方指示实体的介质类型，指定HEAD方法送到接收方的实体介质类型，或GET方法发送的请求介质类型
        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        // 设置打开与此URLConnection引用的资源的通信链接时使用的指定超时值（以毫秒为单位）
        conn.setConnectTimeout(10000);
        // 将读取超时设置为指定的超时时间，以毫秒为单位。
        // conn.setReadTimeout(60000);
        conn.setRequestMethod("GET");
        // Post 请求不能使用缓存
        conn.setUseCaches(false);

        // 建立连接
        conn.connect();
        // 获取响应
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        String result = "";
        while ((line = reader.readLine()) != null) {
            result += line;
        }
        reader.close();
        conn.disconnect();
        System.out.println(result);
        return result;
    } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    return null;

}
}
